<?php

if(isset($_GET['type'])){

	function mysqlQuery($query){
		if(!($iden = mysql_connect("localhost", "root", ""))){
			die("Error: No se puede conectar");
		}

		if(!mysql_select_db("pidb",$iden)){
			die("Error: no existe la tabla");
		}

		$resultado = mysql_query($query);

		if(!$resultado){
			die("Error: no se pudo realizar la consulta");
		}

		mysql_close($iden);

		return $resultado;
	}

	if($_GET['type'] == 'rss'){
		$docType = "rss";
		$mainCont = "description";
		$elementName = "item";
		$rss = true;
	}else if($_GET['type'] == 'atom'){
		$docType = "feed";
		$mainCont = "summary";
		$elementName = "entry";
		$rss = false;
	}

	$doc = new DomDocument();

	$dec = $doc->createElement($docType);
	$dec->setAttribute('version', '2.0');
	if($rss)
		$channel = $doc->createElement('channel');

	$c_title = $doc->createElement('title', "Las ultimas 5 fotos");
	if($rss){
		$c_link = $doc->createElement('link', "http://localhost/PicImg/index.html"); 
	}else{
		$c_link = $doc->createElement('link'); 
		$c_link->setAttribute("href", "http://localhost/PicImg/index.html");
	}

	
	$c_description = $doc->createElement($mainCont, "Las ultimas 5 fotos"); 
	$c_language = $doc->createElement('language', "es"); 
	if(!$rss){
		$c_updated = $doc->createElement('updated', date(DATE_ATOM)); 
		$c_author = $doc->createElement('author');
		$c_author->appendChild($doc->createElement('name', "Martín Candela Calabuig"));
		$c_id = $doc->createElement('id', 'http://localhost/PicImg/index.html');
	}
	
	$doc->appendChild($dec);
	
	if($rss){
		$dec->appendChild($channel);
		$channel->appendChild($c_title);
		$channel->appendChild($c_link);
		$channel->appendChild($c_description);
		$channel->appendChild($c_language);
	}else{
		$dec->appendChild($c_title);
		$dec->appendChild($c_link);
		$dec->appendChild($c_description);
		$dec->appendChild($c_language);
		$dec->appendChild($c_updated);
		$dec->appendChild($c_author);
		$dec->appendChild($c_id);
	}
	$imagenes = mysqlQuery("SELECT * FROM fotos ORDER BY FRegistro DESC LIMIT 5");
	while($row = mysql_fetch_assoc($imagenes)){
		// Tamaño de la imagen
		$imgSize =  getimagesize('http://localhost/PicImg/img/thumbnails/' . $row['Fichero']);
		// Pais del que proviene
		$pais = mysql_fetch_assoc(mysqlQuery("SELECT * FROM paises WHERE IdPais = '{$row['Pais']}'"));
		if( $pais['NomPais'] == "")
			$pais = "--------";
		else
			$pais = $pais['NomPais'];
		
		$tit = $row['Titulo'];
		$lin = 'http://localhost/PicImg/detallesFoto.php?id=' . $row['IdFoto'];

		$des = 'Enviada el '.$row['FRegistro'].', desde '.$pais.'.';

		$c_item = $doc->createElement($elementName); 
		$i_title = $doc->createElement('title', $tit);
		if($rss){
			$i_link = $doc->createElement('link', $lin);
		}else{
			$i_link = $doc->createElement('link');
			$i_link->setAttribute("href", $lin);
			$i_id = $doc->createElement('id', $lin);
			$i_updated = $doc->createElement('updated', $row['FRegistro']);
		}
		$i_description = $doc->createElement($mainCont, $des);

		$c_item->appendChild($i_title);
		$c_item->appendChild($i_link);
		$c_item->appendChild($i_description);

		if($rss){
			$channel->appendChild($c_item);
		}else{
			$c_item->appendChild($i_updated);
			$c_item->appendChild($i_id);
			$dec->appendChild($c_item);
		}

		
	}

	header('content-type: text/xml');
	header('encoding: utf8');
	echo $doc->saveXML();
}
?>